<?php
include 'header.php';
include 'menu.php';
$db = Typecho_Db::get();
$prefix = $db->getPrefix();
$options = Typecho_Widget::widget('Widget_Options');
$books = $db->fetchAll($db->query($db->select()->from($prefix . 'booksline')->order($prefix.'booksline.id', Typecho_Db::SORT_DESC)));
if ($request->op == 'del' && $request->id) {
    if (stripos($request->id, ',') != -1) {
        $ids = explode(',', $request->id);
        var_dump($ids);
        foreach ($ids as $id) {
            $db->query($db->delete($prefix . 'booksline')->where('id = ?', $id));
        }
        $response->redirect(Helper::url('BooksLine%2Fmanage-booksline.php'));
    }
    $db->query($db->delete($prefix . 'booksline')->where('id = ?', $request->id));
    $response->redirect(Helper::url('BooksLine%2Fmanage-booksline.php'));
}
?>

<html>

<head>
    <title>书籍时间线管理后台</title>
</head>

<style>
    .book_lists {
        width: 80%;
    }

    .book_lists thead {
        border-bottom: 2px solid rgba(126, 126, 126, 0.25);
    }

    .book_lists tbody * {
        height: 30px;
        font-size: 14px;
        border-bottom: 2px solid rgba(238, 238, 238, 0.71);
    }

    form input {
        width: 70%;
    }

    body {
        position: relative;
    }

    .book_input {
        background-color: #eee;
        border-top: 2px solid #eee;
        position: fixed;
        width: 720px;
        top: 15%;
        margin: auto auto;
        border: 2px solid red;
        padding-left: 5%;
        padding: auto auto;
        display: none;
    }
</style>


<script>
    function selectAll(obj) {
        let checkboxs = document.getElementsByClassName("select");
        let checked = obj.checked;
        for (i = 0; i < checkboxs.length; i++) {
            if (checked)
                checkboxs.item(i).checked = true;
            else
                checkboxs.item(i).checked = false;
        }
    }


    function showInput() {
        let input = document.getElementById('book_input');
        if (input.style.display == "block")
            input.style.display = "none";
        else
            input.style.display = "block"
    }


    function delAll(url) {
        var flag = confirm("确定删除选中的项？")
        if (flag) {
            let checkboxs = document.getElementsByClassName("select");
            let ids = '';
            for (i = 0; i < checkboxs.length; i++) {
                let item = checkboxs.item(i);
                if (item.checked == true) {
                    ids += item.parentNode.parentNode.childNodes[3].innerText + ',';
                }
            }
            ids = ids.substring(0, ids.length - 1);
            // console.log(ids);
            var http;
            if (window.XMLHttpRequest) {
                //  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
                http = new XMLHttpRequest();
            } else {
                // IE6, IE5 浏览器执行代码
                http = new ActiveXObject("Microsoft.XMLHTTP");
            }

            http.open("GET", url + '&id=' + ids + '&op=del', true);
            http.send();

            http.onreadystatechange = function() {
                if (http.readyState == 4 && http.status == 200) {
                    window.location.reload();
                }

            }

        }
    }
</script>

<body align="center">

    <h3>管理你的书籍吧</h3>

    <div align="left">
        <!--    <span>-->
        <button onclick="delAll('<?php echo Helper::url('BooksLine%2Fmanage-booksline.php'); ?>');" type="button" style="background:#eee;margin-left: 10%;margin-bottom: 3%;border: none;color: rgb(255,70,56)">删除选中项</button>
        <button onclick="showInput();" type="button" style="background:#eee;margin-left: 10%;margin-bottom: 3%;border: none;color: rgb(255,70,56)">添加/编辑书籍</button>
        <!--    </span>-->
    </div>

    <table class="book_lists" align="center">

        <colgroup>
            <col width="50" />
            <col width="50" />
            <col width="100" />
            <col width="200" />
            <col width="150" />
            <col width="100" />
            <col width="150" />
        </colgroup>
        <thead>
            <tr>
                <th><input type="checkbox" onclick="selectAll(this);" /></th>
                <th>ID</th>
                <th>书籍名称</th>
                <th>书籍编著</th>
                <th>书籍描述</th>
                <th>阅读时间</th>
                <th>操作</th>
            </tr>
        </thead>

        <tbody>
            <?php foreach ($books as $book) : ?>
                <tr>
                    <td><input class="select" type="checkbox" /></td>
                    <td><?php echo $book['id']; ?></td>
                    <td><?php echo $book['book_name']; ?></td>
                    <td><?php echo $book['book_author']; ?></td>
                    <td><?php echo $book['book_description']; ?></td>
                    <td><?php echo $book['read_time']; ?></td>
                    <td><a href="<?php echo $request->makeUriByRequest('id=' . $book['id'] . '&op=up'); ?>">编辑</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="<?php echo $request->makeUriByRequest('id=' . $book['id'] . '&op=del'); ?>">删除</a></td>
                </tr>

            <?php endforeach; ?>
        </tbody>


    </table>

    <div class="book_input" id="book_input" align='left'>
        <?php BooksLine_Plugin::form()->render(); ?>
    </div>
</body>

</html>